package com.sky.myapplication.kotlin.kotlinAdapter

import android.content.Context
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.viewpager.widget.PagerAdapter
import com.sky.myapplication.R

/**
 * @Author: Minxp
 * @UpdateDate: 2021/7/7 0007
 * @Description: java类作用描述
 */
class MainPageAdapter constructor(context: Context, data: ArrayList<String>): PagerAdapter() {
    var context: Context = context
    var mData: List<String> = data

    override fun getCount(): Int {
        return mData.size
    }

    override fun instantiateItem(container: ViewGroup, position: Int): Any {
        var view = View.inflate(context, R.layout.item_view_page, null)
        var tvName = view.findViewById<TextView>(R.id.tv_name)

        tvName.text = mData.get(position)

        container.addView(view)
        return view
    }

    override fun getPageTitle(position: Int): CharSequence? {
        return mData[position]
    }

    override fun isViewFromObject(view: View, `object`: Any): Boolean {
        return view == `object`
    }

    override fun destroyItem(container: ViewGroup, position: Int, `object`: Any) {
//        super.destroyItem(container, position, `object`)
        //kotlin中的强转as，java中的强转（View）
        container.removeView(`object` as View?)
    }
}